package com.hs.ph.Servlet;

import com.alibaba.fastjson.JSON;
import com.hs.ph.Dao.ShopDao;
import com.hs.ph.Dao.impl.ShopDaoImpl;
import com.hs.ph.Entity.Result;
import com.hs.ph.Entity.Shop;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/shop")
public class ShopServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        ShopDao shopDao = new ShopDaoImpl();
        if("selectAll".equals(action)){
            Map<String,String> map = new HashMap<>();
            String shopName = req.getParameter("keyword");
            String shopPhone = req.getParameter("phone");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            map.put("keyword",shopName);
            map.put("shopPhone",shopPhone);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);

            List<Shop> list = shopDao.selectAll(map);
            int total = shopDao.selectCount(map);

            Map map1=new HashMap<>();

            map1.put("total",total);
            map1.put("list",list);

            Result result = Result.success(map1);
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("insert".equals(action)) {
            String shopName = req.getParameter("shopName");
            String Id = req.getParameter("shopId");
            int shopId=0;
            if(Id!=null && !"".equals(Id)){
                shopId = Integer.parseInt(Id);
            }
            String shopDescribe = req.getParameter("shopDescribe");
            String shopImage = req.getParameter("shopImage");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
            String shopCity = req.getParameter("shopCity");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopAddress = req.getParameter("shopAddress");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopLatitude = req.getParameter("shopLatitude");
            String Status = req.getParameter("shopStatus");
            int shopStatus=0;
            if(Status!=null && !"".equals(Status)){
                shopStatus = Integer.parseInt(Status);
            }

            String UserId = req.getParameter("shopUserId");
            int shopUserId=0;
            if(UserId!=null && !"".equals(UserId)){
                shopUserId = Integer.parseInt(UserId);
            }
            String shopAccount = req.getParameter("shopAccount");
            String shopPassword = req.getParameter("shopPassword");
            String shopSort = req.getParameter("shopSort");
            String Volume = req.getParameter("salesVolume");
            int salesVolume=0;
            if(Volume!=null && !"".equals(Volume)){
                salesVolume = Integer.parseInt(Volume);
            }
            String distance = req.getParameter("distance");
            String shopCommunitySort = req.getParameter("shopCommunitySort");
            String concernstatus = req.getParameter("concernStatus");
            int concernStatus=0;
            if(concernstatus!=null && !"".equals(concernstatus)){
                concernStatus = Integer.parseInt(concernstatus);
            }
            String shopOrdercount = req.getParameter("shopOrderCount");
            int shopOrderCount=0;
            if(shopOrdercount!=null && !"".equals(shopOrdercount)){
                shopOrderCount = Integer.parseInt(shopOrdercount);
            }
            String shopCategoryList = req.getParameter("shopCategoryList");
            String shopGoodsList = req.getParameter("shopGoodsList");
            Shop shop = new Shop(
             shopId,  shopName,  shopDescribe,  shopImage,  shopPhone,  shopProvince,  shopCity,  shopDistrict,  shopAddress,  shopLongitude,  shopLatitude,
                    shopStatus,  shopUserId,  shopAccount,  shopPassword,  shopSort,
                    salesVolume,  distance,  shopCommunitySort,  concernStatus,  shopOrderCount,
                    shopCategoryList,  shopGoodsList
            );

            int num = shopDao.insert(shop);
            if (num!=0){
                Result result = Result.success("添加成功");
                resp.getWriter().write(JSON.toJSONString(result));
            }else{
                Result result = Result.error("添加失败");
                resp.getWriter().write(JSON.toJSONString(result));
            }


        } else if ("update".equals(action)) {

            String shopName = req.getParameter("shopName");
            String Id = req.getParameter("shopId");
            int shopId=0;
            if(Id!=null && !"".equals(Id)){
                shopId = Integer.parseInt(Id);
            }
            String shopDescribe = req.getParameter("shopDescribe");
            String shopImage = req.getParameter("shopImage");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
            String shopCity = req.getParameter("shopCity");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopAddress = req.getParameter("shopAddress");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopLatitude = req.getParameter("shopLatitude");
            String Status = req.getParameter("shopStatus");
            int shopStatus=0;
            if(Status!=null && !"".equals(Status)){
                shopStatus = Integer.parseInt(Status);
            }

            String UserId = req.getParameter("shopUserId");
            int shopUserId=0;
            if(UserId!=null && !"".equals(UserId)){
                shopUserId = Integer.parseInt(UserId);
            }
            String shopAccount = req.getParameter("shopAccount");
            String shopPassword = req.getParameter("shopPassword");
            String shopSort = req.getParameter("shopSort");
            String Volume = req.getParameter("salesVolume");
            int salesVolume=0;
            if(Volume!=null && !"".equals(Volume)){
                salesVolume = Integer.parseInt(Volume);
            }
            String distance = req.getParameter("distance");
            String shopCommunitySort = req.getParameter("shopCommunitySort");
            String concernstatus = req.getParameter("concernStatus");
            int concernStatus=0;
            if(concernstatus!=null && !"".equals(concernstatus)){
                concernStatus = Integer.parseInt(concernstatus);
            }
            String shopOrdercount = req.getParameter("shopOrderCount");
            int shopOrderCount=0;
            if(shopOrdercount!=null && !"".equals(shopOrdercount)){
                shopOrderCount = Integer.parseInt(shopOrdercount);
            }
            String shopCategoryList = req.getParameter("shopCategoryList");
            String shopGoodsList = req.getParameter("shopGoodsList");
            Shop shop = new Shop(
                    shopId,  shopName,  shopDescribe,  shopImage,  shopPhone,  shopProvince,  shopCity,  shopDistrict,  shopAddress,  shopLongitude,  shopLatitude,
                    shopStatus,  shopUserId,  shopAccount,  shopPassword,  shopSort,
                    salesVolume,  distance,  shopCommunitySort,  concernStatus,  shopOrderCount,
                    shopCategoryList,  shopGoodsList
            );

            int num = shopDao.update(shop);
            if (num!=0){
                Result result = Result.success("修改成功");
                resp.getWriter().write(JSON.toJSONString(result));
            }else{
                Result result = Result.error("修改失败");
                resp.getWriter().write(JSON.toJSONString(result));
            }

        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }


}
